#pragma once
#include <iostream>
#include <string>
#include <ctemplate/template.h>

#include "oj_model.hpp"

namespace oj_view
{
    using namespace ns_model;
    const std::string &template_html="./template_html/";

    class View
    {    
    public:
        void AllExpandHtml(const vector<struct Question>&questions,std::string* html)
        {
            //渲染，的目标 html文件路径
            std::string scr_html=template_html+"all_questions.html";
            //形成字典树
            ctemplate:: TemplateDictionary root("all_questions");  //unordered_map<> all_questions

            for(const auto& q:questions)
            {
                ctemplate::TemplateDictionary::*sub=root.AddSectionDictionary("question_list");
                sub->SetValue("number",q.number);
                sub->SetValue("title",q.title);
                sub->SetValue("star",q.star);
            }

            //获得
            ctemplate::Template *tpl=ctemplate::Template::GetTemplate(scr_html,ctemplate::DO_NOT_STRIP);

            tpl->Expand(html,&root);
        }

        void OneExpandHtml(const struct Question&q,std::string *html)
        {
            //形成路径
            std::string src_html=template_html+"one_question";
            
            //形成字典
            ctemplate::TemplateDictionary root("one_question");
            root.SetValue("number",q.number);
            root.Setvalue("title",q.title);
            root.SetValue("star",q.star);
            root.SetValue("desc",q.desc);
            root.SetValue("pre_code",q.header);

            // 获得被渲染的html
            ctemplate::Template *tpl=ctemplate::Template::GetTemplate(src_html,ctemplate::DO_NOT_STRIP);
            
            //开始渲染功能
            tpl->Expand(html,&root);
        }
    };
};